package com.ysg.widget.refresh;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import androidx.recyclerview.widget.RecyclerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.ysg.R;
import com.ysg.utils.YCollectionUtil;
import com.ysg.utils.YRecyclerViewUtil;
import com.ysg.widget.layout.YStateLayout;
import java.util.List;

/**
 * 刷新控件
 */
public class YRefreshView extends LinearLayout {
    private SmartRefreshLayout refreshLayout;
    private YStateLayout stateLayout;
    private RecyclerView recyclerView;

    public YRefreshView(Context context) {
        super(context);
    }

    public YRefreshView(Context context, AttributeSet attrs) {
        super(context, attrs);

        View view = View.inflate(context, R.layout.widget_refresh, this);
        refreshLayout = view.findViewById(R.id.refreshLayout);
        stateLayout = view.findViewById(R.id.stateLayout);
        recyclerView = view.findViewById(R.id.recyclerView);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.YRefreshView);
        int refreshType = array.getInt(R.styleable.YRefreshView_refreshType, 0);
        switch (refreshType){
            case 1:
                //只能下拉功能
                refreshLayout.setEnableLoadMore(false);
                break;
            case 2:
                //没有刷新, 下拉
                refreshLayout.setEnableRefresh(false);
                refreshLayout.setEnableLoadMore(false);
                break;
            case 3:
                //没有刷新功能
                refreshLayout.setEnableRefresh(false);
                break;
        }
        array.recycle();
    }

    public void initAdapter(BaseQuickAdapter adapter){
        YRecyclerViewUtil.init(getContext(), recyclerView, adapter);
    }

    public void initAdapterNoLine(BaseQuickAdapter adapter){
        YRecyclerViewUtil.initNoLine(getContext(), recyclerView, adapter);
    }

    public void initAdapterGrid(BaseQuickAdapter adapter, int count){
        YRecyclerViewUtil.initGrid(getContext(), recyclerView, adapter, count);
    }

    public void initAdapterStagger(BaseQuickAdapter adapter, int count){
        YRecyclerViewUtil.initStagger(getContext(), recyclerView, adapter, count);
    }

    public void setAdapterUI(BaseQuickAdapter adapter, List list, boolean refresh) {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
        if (YCollectionUtil.isEmpty(list)) {
            refreshLayout.finishLoadMoreWithNoMoreData();
            if (refresh){
                stateLayout.showEmpty();
            }
            return;
        }

        stateLayout.showContent();
        if (refresh){
            adapter.setList(list);
        }else{
            adapter.addData(list);
        }
    }

    public SmartRefreshLayout getRefreshLayout(){
        return refreshLayout;
    }





}
